package com.zhxh.nzpos.dao;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.zhxh.nzpos.db.DBHelper;
import com.zhxh.nzpos.model.Authority;
import com.zhxh.nzpos.utils.RandomStringUtils;

/**
 * Authority DAO
 * 
 * @author Administrator
 * 
 */
public class AuthorityDao {

	DBHelper dbHelper;

	private final static String LOG_TAG = "AuthorityDao";
	protected static final String TBL_NAME = "authority";

	public AuthorityDao(Context context) {
		dbHelper = new DBHelper(context);
	}

	/**
	 * add new authority
	 * 
	 * @param authority
	 */
	public String save(Authority authority) {
		Log.i(LOG_TAG, "AuthorityDao  save " + authority.toString());
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		authority.setId(RandomStringUtils.randomNumeric(10));
		long time = System.currentTimeMillis();
		authority.setCreated(time);
		authority.setModified(time);
		db.beginTransaction();// 开始事务
		try {
			db.execSQL(
					"insert into authority(ID,Limits,created,modified) values(?,?,?,?)",
					new Object[] { authority.getId(), authority.getLimits(),
							authority.getCreated(), authority.getModified() });

			db.setTransactionSuccessful();
		} catch (Exception e) {
			Log.i("AuthorityDao  save", e.getMessage());
		}
		db.endTransaction();// 事务结束;
		dbHelper.close();
		return authority.getId();
	}

	/**
	 * update a authority
	 * 
	 * @param authority
	 */

	public void update(Authority authority) {
		Log.i(LOG_TAG, "AuthorityDao  update " + authority.toString());
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		authority.setModified(System.currentTimeMillis());
		db.beginTransaction();// 开始事务
		try {
			db.execSQL(
					"update authority set Limits=?,modified=? where ID=?",
					new Object[] { authority.getLimits(),
							authority.getModified(), authority.getId() });

			// db.setTransactionSuccessful();
		} catch (Exception e) {
			Log.i("AuthorityDao  update", e.getMessage());
		}
		db.endTransaction();// 事务结束;
		dbHelper.close();

	}

	/**
	 * get a authority by id
	 * 
	 * @param i
	 * @return
	 */
	public Authority find(String id) {
		Log.i(LOG_TAG, "AuthorityDao  find " + id);
		Authority authority = null;
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		Cursor cursor = db.rawQuery(
				"select ID,Limits,created,modified from authority where ID=?",
				new String[] { id });
		if (cursor.moveToNext()) {
			authority = new Authority();

			authority.setId(cursor.getString(cursor.getColumnIndex("ID")));
			authority.setLimits(cursor.getString(1));
			authority.setCreated(cursor.getLong(2));
			authority.setModified(cursor.getLong(3));
			
		}
		cursor.close();
		dbHelper.close();
		return authority;
	}

	/**
	 * delete a authority by id
	 * 
	 * @param id
	 */
	public void delete(String id) {
		Log.i(LOG_TAG, "AuthorityDao  delete " + id);
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		db.execSQL("delete from authority where ID=?", new Object[] { id });
		dbHelper.close();
	}

	/**
	 * delete all
	 * 
	 * @param id
	 */
	public void deleteAll() {
		Log.i(LOG_TAG, "CardDao  delete all from  card: ");
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		db.execSQL("delete  from authority");
		dbHelper.close();
	}

	/**
	 * fetch authoritys
	 * 
	 * @param pageNumber
	 * @param pageSize
	 * @return
	 */
	public List<Authority> getScrollData(int pageNumber, int pageSize) {
		Log.i(LOG_TAG, "AuthorityDao  find data  ");

		List<Authority> authoritys = new ArrayList<Authority>();
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		Cursor cursor = db.rawQuery(
				"select ID,Limits,created,modified from authority order by modified desc limit ?,?",
				new String[] { String.valueOf((pageNumber - 1) * pageSize),
						String.valueOf(pageSize) });// pageNumber页码
		while (cursor.moveToNext()) {// pageSize每页获取的记录数
			Log.i("aa", "333333333333");
			Authority authority = new Authority();
			authority.setId(cursor.getString(cursor.getColumnIndex("ID")));
			authority.setLimits(cursor.getString(1));
			authority.setCreated(cursor.getLong(2));
			authority.setModified(cursor.getLong(3));
			authoritys.add(authority);
		}
		cursor.close();
		dbHelper.close();
		return authoritys;
	}

	/**
	 * fetch all authoritys
	 * 
	 * @return
	 */
	public List<Authority> getAll() {
		Log.i(LOG_TAG, "AuthorityDao  find data  ");

		List<Authority> authoritys = new ArrayList<Authority>();
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		Cursor cursor = db.rawQuery(
				"select ID,Limits,created,modified from authority  order by modified desc", null);// pageNumber页码
		while (cursor.moveToNext()) {// pageSize每页获取的记录数
			Authority authority = new Authority();
			authority.setId(cursor.getString(cursor.getColumnIndex("ID")));
			authority.setLimits(cursor.getString(1));
			authority.setCreated(cursor.getLong(2));
			authority.setModified(cursor.getLong(3));
			authoritys.add(authority);
		}
		cursor.close();
		dbHelper.close();
		return authoritys;
	}

	/**
	 * get authority's count
	 * 
	 * @return
	 */
	public long getCount() {
		Log.i(LOG_TAG, "AuthorityDao  getCount ");
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		long count = DatabaseUtils.queryNumEntries(db, TBL_NAME);
		dbHelper.close();
		return count;
	}

}
